package com.pozu.new_retail.controllers;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pozu.new_retail.dao.base.JsonResult;
import com.pozu.new_retail.dao.model.Message;
import com.pozu.new_retail.dao.model.Ugc;
import com.pozu.new_retail.dao.model.Upvote;
import com.pozu.new_retail.service.MessageService;
import com.pozu.new_retail.service.UgcService;
import com.pozu.new_retail.service.UpvoteService;
import com.pozu.new_retail.utils.CodeConstants;
import com.pozu.new_retail.utils.CodeProperties;
import com.pozu.new_retail.utils.Tools;

@Controller
public class UpvoteController {
	
	@Autowired
	private UpvoteService upvoteService;
	@Autowired
	private UgcService ugcService;
	@Autowired
	private MessageService messageService;
		
	@ResponseBody
	@RequestMapping(value = "/api/upvotes", method = RequestMethod.POST)
	public Object addUpvote(HttpServletRequest request) throws Exception{
		
		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		Long userId = Long.parseLong(request.getHeader("userId"));
		
		Upvote requestUpvote = Tools.getRequestObject(request, Upvote.class);
		
		JsonResult jr = new JsonResult();
		
		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("type", requestUpvote.getType());
		map.put("otherId", requestUpvote.getOtherId());
		map.put("userId", userId);
		map.put("appId", appId);
		
		Upvote vote = upvoteService.queryByMap(map);
		
		if(null!=vote){
			if(vote.getState()==0){
				jr.respCode = CodeConstants.UPVOTEEXISTED_MSG;
				jr.respMsg = CodeProperties.getString(CodeConstants.UPVOTEEXISTED_MSG);
				return jr;
			}else{		
				
				vote.setState(0);
				upvoteService.updateUpvote(vote);
				
				jr.respCode = CodeConstants.SUCCESS_CODE;
				jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
				return jr;
			}
		}
		
		requestUpvote.setUserId(userId);
		requestUpvote.setAppId(appId);

		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		upvoteService.saveUpvote(requestUpvote);
		
		if (requestUpvote.getType() == 0) {
			
			Ugc ugc = ugcService.queryInfoByUgcId(requestUpvote.getOtherId());
			
			Message message = new Message();
			
			message.setUserId(ugc.getUserId());
			message.setType(0);
			message.setOtherId(requestUpvote.getId()+"");
			message.setAppId(appId);
			messageService.saveMessage(message);
			
		}
		
		return jr;
	}	
	
	@ResponseBody
	@RequestMapping(value = "/api/upvotes", method = RequestMethod.PUT)
	public Object updateUpvote(HttpServletRequest request) throws Exception{
		
		int userId = Integer.parseInt(request.getHeader("userId"));
		
		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		Upvote requestUpvote = Tools.getRequestObject(request, Upvote.class);
		
		JsonResult jr = new JsonResult();
		
		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("type", requestUpvote.getType());
		map.put("otherId", requestUpvote.getOtherId());
		map.put("userId", userId);
		map.put("appId", appId);
		
		Upvote vote = upvoteService.queryByMap(map);
		
		if(null!=vote){
			requestUpvote.setState(1);
			requestUpvote.setId(vote.getId());
			upvoteService.updateUpvote(requestUpvote);
		}
		
		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		
		return jr;
	}	
}
